package com.nbst.service.base.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nbst.comnutil.LogOut;
import com.nbst.comnutil.NormalResult;
import com.nbst.comnutil.UpdateShiroPermi;
import com.nbst.dao.mapper.mes.MenuMapper;
import com.nbst.model.base.Base;
import com.nbst.model.base.Menu;
import com.nbst.service.base.IMenuService;

/**
 * @ClassName: PermissionsServiceImpl
 * @Description:权限管理Service实现层
 * @author 兵
 * @date 2017-8-17
 * 
 */
@Service
public class MenuServiceImpl implements IMenuService {
	@Autowired
	MenuMapper menuMapper;

	@Autowired
	LogOut logout;

	@Override
	public NormalResult deleteByPrimaryKey(String pId) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = menuMapper.deleteByPrimaryKey(pId);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("删除成功");
				// 清空权限缓存
				UpdateShiroPermi.cleanShiro();
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("删除失败");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public NormalResult insertSelective(Menu record) {
		NormalResult result = new NormalResult();
		record.setfDate(new Date());
		int num = 0;
		try {
			num = menuMapper.insertSelective(record);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("新增成功");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("新增失败");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public List<Menu> findByCondition(Map<String,Object> map) {
		List<Menu> list = new ArrayList<Menu>();
		try {
			list = menuMapper.findByCondition(map);
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

	@Override
	public NormalResult updateByPrimaryKeySelective(Menu record) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = menuMapper.updateByPrimaryKeySelective(record);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("修改成功");
			} else {
				result.setCode("9999");
				result.setMessage("修改失败");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("出错了,刷新后再试试吧");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public List<Base> getRolePermissionList(Map<String,Object> map) {
		List<Base> list = new ArrayList<Base>();
		try {
			list = menuMapper.getRolePermissionList(map);
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

	@Override
	public List<Menu> getDeskPermissionList(Map<String,Object> map) {
		List<Menu> list = new ArrayList<Menu>();
		try {
			list = menuMapper.getDeskPermissionList(map);
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

	@Override
	public List<Menu> findByUid(Map<String,Object> map) {
		List<Menu> list = new ArrayList<Menu>();
		try {
			list = menuMapper.findByUid(map);
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

}